package com.fwj.user.web.rbac;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.user.api.rbac.RBACRoleService;
import com.fwj.user.entity.rbac.role.Role;
import com.fwj.user.entity.user.UserBaseInfo;
import com.fwj.user.model.rbac.role.RoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: fwj
 * @Data: 2022/5/1 17:29
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/RBACRole")
public class RBACRoleController {

    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(RBACRoleController.class);

    @Autowired
    private RBACRoleService roleService;

    /**
     * @Description TODO 保存角色，添加或修改
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/saveRole")
    public JSONObject saveRole(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            RoleVo roleVo = JSONObject.parseObject(jsonObject.toJSONString(),RoleVo.class);
            Role role = buildRole(roleVo,userBaseInfo);
            return roleService.saveRole(role);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    private Role buildRole(RoleVo roleVo,UserBaseInfo userBaseInfo) {
        Role role = new Role();
        role.setRoleName(roleVo.getRoleName());
        role.setCreator(userBaseInfo.getId());
        return role;
    }

    /**
     * @Description TODO 删除角色
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/deleteRole")
    public JSONObject deleteRole(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            String roleNumber = jsonObject.getString("roleNumber");
            return roleService.deleteRole(roleNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


    /**
     * @Description TODO 查询角色
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryRole")
    public JSONObject queryRole(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            return roleService.queryRole(jsonObject);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


    /**
     * @Description TODO 获取角色详情
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/getRoleDetails")
    public JSONObject getRoleDetails(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            String roleNumber = jsonObject.getString("roleNumber");
            return roleService.getRoleDetails(roleNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

}
